package com.baymax.test.scene

import org.junit.Test
import java.util.*

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene
 * @class:       sorts
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2023/3/28 14:20
 * @Copyright (C) 2023 YSTEN
 * @author:       xuyujian
 */

data class VideoPoint(var start: Int, var end: Int) {


    companion object {
         fun mergePoints(points: List<VideoPoint>) {
            println("mergePoints $points")
            if (points.isEmpty()) {
                return
            }

            val len = points.size
            val starts = IntArray(len)
            val ends = IntArray(len)
            //将首尾整数分别放置在两个数组中
            for (i in 0 until len) {
                val start = points[i].start
                val end = points[i].end
                if (start >= end) {
                    starts[i] = end;
                    ends[i] = start;
                } else {
                    starts[i] = start
                    ends[i] = end
                }
            }

            // 对两个数组进行排序
            // 对两个数组进行排序
            Arrays.sort(starts)
            Arrays.sort(ends)
            // 合并重叠区间
             val result  = mutableListOf<VideoPoint>()
            var i = 0
            var j = 0
            while (i < len) {
                if (i == len - 1 || starts[i + 1] > ends[i]) {
                    result.add(VideoPoint(starts[j] ,ends[i]))
                    j = i + 1
                }
                i++
            }
            println("mergePoints $result")
        }
    }

}

fun merge(intervals: Array<IntArray>): Array<IntArray> {

    val len = intervals.size
    val starts = IntArray(len)
    val ends = IntArray(len)
    //将首尾整数分别放置在两个数组中
    for (i in 0 until len) {
        val start = intervals[i][0]
        val end = intervals[i][1]
        if (start >= end) {
            starts[i] = end;
            ends[i] = start;
        } else {
            starts[i] = start
            ends[i] = end
        }
    }

    // 对两个数组进行排序
    // 对两个数组进行排序
    Arrays.sort(starts)
    Arrays.sort(ends)
    // 合并重叠区间
    val result  = arrayListOf<IntArray>()
    var i = 0
    var j = 0
    while (i < len) {
        if (i == len - 1 || starts[i + 1] > ends[i]) {
            result.add(intArrayOf(starts[j] ,ends[i]))
            j = i + 1
        }
        i++
    }
    return result.toTypedArray()
}

class Tests {
    @Test
    fun `testSorts`() {
        VideoPoint.mergePoints(listOf(VideoPoint(5, 1), VideoPoint(4, 10), VideoPoint(8, 9)))
        VideoPoint.mergePoints(listOf( VideoPoint(4, 10), VideoPoint(8, 9) ,VideoPoint(2, 1),))
        println(merge(arrayOf(intArrayOf(5, 1), intArrayOf(4, 10), intArrayOf(8, 9))).contentDeepToString())
        println(merge(arrayOf( intArrayOf(4, 10), intArrayOf(8, 9) ,intArrayOf(2, 1))).contentDeepToString())

    }

}
